var createError = require('http-errors');
var express = require('express');
var path = require('path');
var cookieParser = require('cookie-parser');
var logger = require('morgan');
var indexRouter = require('./routes/index');
var usersRouter = require('./routes/users');
var app = express()

var webSocketConfig = require('./util/webSocket.js')

// try {
//     webSocketConfig.wsConnect()
//     console.log('数据库连接成功')
// } catch (error) {
//     console.log('数据库连接失败-------')
// }
// var app = webSocketConfig.app
// var wss = webSocketConfig.wss

// view engine setup
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'jade');

app.use(logger('dev'));
app.use(express.json());
app.use(express.urlencoded({ extended: false }));
app.use(cookieParser());
app.use(express.static(path.join(__dirname, 'public')));

app.all('*', function(req, res, next) {
    res.header("Access-Control-Allow-Origin", "*");
    res.header('Access-Control-Allow-Credentials', true);
    res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
    res.header("X-Powered-By",' 3.2.1')
    next();
});


app.use('/api', indexRouter);
app.use('/users', usersRouter);

// catch 404 and forward to error handler
app.use(function(req, res, next) {
  next(createError(404));
});

// error handler
app.use(function(err, req, res, next) {
  // set locals, only providing error in development
  res.locals.message = err.message;
  res.locals.error = req.app.get('env') === 'development' ? err : {};

  // render the error page
  res.status(err.status || 500);
  res.render('error');
});





  

// this.wsServer.on('connection', function (ws, req) {

//   if (!req.headers.cookie) {
//     console.log('没有用户,连接聊天室失败')
//     ws.send('没有用户,连接聊天室失败')
//     return
//   } else {
//     console.log('websocket连接成功')
//   }
//   let cookie = get_cookies(req)

//   ws.username = cookie.username
//   online_userlist.push(ws.username)
//   console.log('websocket连接成功')
//   //join the chat will broadcast a welcome
//   this.broadcast('join', online_userlist, `欢迎${ws.username}进入聊天室`)

//   //接受消息并发送给所有人
//   ws.on('message', function (message) {
//     this.broadcast('normal', ws.username, message)
//   })

//   //关闭连接时提示其他用户
//   ws.on('close', function () {
//     var userIndex = online_userlist.indexOf(ws.username)
//     if (userIndex >= 0) {
//       online_userlist.splice(userIndex, 1)
//       _this.broadcast('leave', ws.username, `用户${ws.username}离开了聊天室`)
//     }
//   })
// })


// 开始连接
// wss.on('connection', function(ws,req){
//   if(!req.headers.cookie){
//     console.log('没有用户,连接聊天室失败')
//     return
//   }else{
//     console.log(req)
//   }
//   let cookie = get_cookies(req)

//   ws.username = cookie.username
//   online_userlist.push(ws.username)

//   //join the chat will broadcast a welcome
//   wss.broadcast('join',online_userlist,`欢迎${ws.username}进入聊天室`)


//   ws.on('message',function(message){
//     wss.broadcast('normal',ws.username,message) 
//   })

//   ws.on('close',function(){
//     var userIndex = online_userlist.indexOf(ws.username)

//     if(userIndex>=0){
//       online_userlist.splice(userIndex,1)
//       wss.broadcast('leave',ws.username,`用户${ws.username}离开了聊天室`)
//     }

//   })
// })


module.exports = app
